<!--公用组件：消息内容展示，实现群聊和单聊业务-->
<template>
  <div id="mainContent">
    <div class="top-panel" ref="topPanel">
      <div class="title-panel">
        <p v-if="messageStatus === 0">{{ buddyName }}</p>
        <p v-else>{{ buddyName }}({{ onlineUsers }})</p>
        <!--在线设备类型-->
        <!--<div class="equipmentType">
                    <img :src="this.resourceObj.phoneNormal" alt="">
                </div>-->
      </div>
      <div class="operate-panel">
        <div class="ico-panel">
          <div
            ref="createDisSrcPanel"
            class="item-panel"
            @mouseenter="createDisEvent('hover')"
            @mouseleave="createDisEvent('leave')"
            @click="createDisEvent('click')"
          >
            <img :src="createDisSrc" alt="" draggable="false"/>
          </div>
        </div>
      </div>
    </div>
    <!--消息显示-->
    <div class="messages-panel transparent-scroll-bar" ref="messagesContainer">
      <div class="loading-msg-panel" v-if="isLoading">
        <img
          src="@/assets/img/Translate_AIO_Loading@2x.png"
          alt="加载消息"
          draggable="false"
        />
      </div>
      <div
        class="row-panel"
        v-for="(item, index) in senderMessageList"
        :style="{ visibility: msgShowStatus }"
        :key="item.id"
      >
        <!--消息发送时间:当前发送消息为第一条显示时间-->
        <div class="sender-time-panel" v-if="index === 0">
          <!--非今天的消息显示日期与时间-->
          <span
            v-if="
              Date.parse(serverTime.substring(0, 10)) -
                Date.parse(item.createTime.substring(0, 10)) >=
                86400000
            "
          >{{ item.createTime.substring(5, 16) }}</span
          >
          <!--今天的消息显示时间-->
          <span v-else>{{ item.createTime.substring(10, 16) }}</span>
        </div>
        <!--当前消息与上一条消息发送时间间隔1分钟就显示-->
        <div
          class="sender-time-panel"
          v-else-if="
            Date.parse(String(item.createTime)) -
              Date.parse(String(senderMessageList[index - 1].createTime)) >=
              60000
          "
        >
          <!--非今天的消息显示日期与时间-->
          <span
            v-if="
              Date.parse(serverTime.substring(0, 10)) -
                Date.parse(item.createTime.substring(0, 10)) >=
                86400000
            "
          >{{ item.createTime.substring(5, 16) }}</span
          >
          <!--今天的消息显示时间-->
          <span v-else>{{ item.createTime.substring(10, 16) }}</span>
        </div>
        <!--发送者消息样式-->
        <div
          class="sender-panel"
          v-if="item.userId === userID"
          :data-createTime="item.createTime"
        >
          <!--昵称展示-->
          <div class="user-name-panel sender">
            <p>{{ item.userName }}</p>
          </div>
          <!--消息-->
          <div class="msg-body">
            <!--消息尾巴-->
            <div class="tail-panel">
              <svg class="icon" aria-hidden="true">
                <use
                  xlink:href="#icon-zbds30duihuakuangyou"
                  color="#dce7dc"
                ></use>
              </svg>
            </div>
            <!--消息内容-->
            <p v-html="item.msgText"/>
          </div>
          <!--头像-->
          <div class="avatar-panel">
<!--            <img :src="item.avatar" alt="" draggable="false"/>-->
          </div>
        </div>
        <!--对方消息样式-->
        <div class="otherSide-panel" v-else :data-createTime="item.createTime">
          <!--头像-->
          <div class="avatar-panel">
<!--            <img :src="item.avatarSrc" alt="" draggable="false"/>-->
          </div>
          <!--昵称展示-->
          <div class="user-name-panel sender">
            <p>{{ item.userName }}</p>
          </div>
          <!--消息-->
          <div class="msg-body">
            <!--消息尾巴-->
            <div class="tail-panel">
              <svg class="icon" aria-hidden="true">
                <use xlink:href="#icon-zbds30duihuakuangzuo"></use>
              </svg>
            </div>
            <!--消息内容-->
            <p v-html="item.msgText"/>
          </div>
        </div>
      </div>
    </div>
    <!--用户输入模块-->
    <div class="user-input-panel" @click="getEditableDivFocus()">
      <div class="toolbar-panel">
        <div class="item-panel" v-for="item in toolbarList" :key="item.info">
          <div v-if="item.name === 'selectImg'" class="file-panel">
            <img
              class="emoticon"
              ref="selectImg"
              :alt="item.info"
              draggable="false"
            />
            <input
              class="file"
              name="file"
              type="file"
              accept="image/png,image/gif,image/jpeg"
              @mouseenter="
                toolbarSwitch(
                  'hover',
                  $event,
                  item.src,
                  item.hover,
                  item.down,
                  item.name
                )
              "
              @mouseleave="
                toolbarSwitch(
                  'leave',
                  $event,
                  item.src,
                  item.hover,
                  item.down,
                  item.name
                )
              "
              @mousedown="
                toolbarSwitch(
                  'down',
                  $event,
                  item.src,
                  item.hover,
                  item.down,
                  item.name
                )
              "
              @mouseup="
                toolbarSwitch(
                  'up',
                  $event,
                  item.src,
                  item.hover,
                  item.down,
                  item.name
                )
              "
              @change="sendImage($event)"
            />
          </div>
          <img
            v-else
            class="emoticon"
            draggable="false"
            @mouseenter="
              toolbarSwitch(
                'hover',
                $event,
                item.src,
                item.hover,
                item.down,
                item.name
              )
            "
            @mouseleave="
              toolbarSwitch(
                'leave',
                $event,
                item.src,
                item.hover,
                item.down,
                item.name
              )
            "
            @mousedown="
              toolbarSwitch(
                'down',
                $event,
                item.src,
                item.hover,
                item.down,
                item.name
              )
            "
            @mouseup="
              toolbarSwitch(
                'up',
                $event,
                item.src,
                item.hover,
                item.down,
                item.name
              )
            "
            :alt="item.info"
          />
        </div>
      </div>
      <div
        id="msgInputContainer"
        class="input-panel"
        ref="msgInputContainer"
        @keypress.enter.exact="sendMessage($event)"
        contenteditable="true"
        spellcheck="false"
      ></div>
      <!--表情面板-->
      <div
        class="emoticon-panel"
        :style="{ display: emoticonShowStatus }"
        ref="emoticonPanel"
      >
        <div class="container transparent-scroll-bar">
          <div class="row-panel">
            <div class="item-panel" v-for="item in emojiList" :key="item.info">
              <img
                :src="require(`@/assets/img/emoji/${item.src}`)"
                :alt="item.info"
                draggable="false"
                @mouseover="
                  emojiConversion(
                    $event,
                    'over',
                    item.src,
                    item.hover,
                    item.info
                  )
                "
                @mouseleave="
                  emojiConversion(
                    $event,
                    'leave',
                    item.src,
                    item.hover,
                    item.info
                  )
                "
                @click="
                  emojiConversion(
                    $event,
                    'click',
                    item.src,
                    item.hover,
                    item.info
                  )
                "
              />
            </div>
          </div>
        </div>
        <div class="ico-panel"></div>
      </div>
    </div>
    <!--截图组件-->
    <screen-short
      v-if="screenshortStatus"
      @destroy-component="destroyComponent"
      @get-image-data="getImg"
    ></screen-short>
    <!--查看大图组件-->
    <show-img
      v-if="showImgStatus"
      :src="imgSrc"
      @destroy-component="destroyShowImg"
    ></show-img>
  </div>
</template>

<script>

  export default {
    name: 'message-display',
    components: {},
    props: {}

  }
</script>

<style lang="scss"scoped>
  #mainContent {
    width: 100%;
    height: 100%;

    .top-panel {
      width: 100%;
      height: 30px;
      display: flex;
      align-items: center;
      padding: 4px;
      box-sizing: border-box;

      .title-panel {
        width: 70%;
        height: 25px;
        display: flex;
        align-items: center;
        padding: 6px 0 0 6px;

        .equipmentType {
          width: 18px;
          height: 18px;
          margin-left: 5px;

          img {
            width: 100%;
            height: 100%;
            user-select: none;
          }
        }
      }

      /*操作栏样式:单聊*/
      .operate-panel {
        width: 29%;
        height: 25px;

        .ico-panel {
          width: 100%;
          height: 100%;
          display: flex;
          justify-content: flex-end;
          align-items: center;

          .item-panel {
            width: 20px;
            height: 20px;
            padding-right: 10px;

            img {
              width: 100%;
              height: 100%;
              user-select: none;
            }
          }
        }
      }

      /*操作栏样式:群聊*/
      .operate-group-panel {}
    }

    .messages-panel {
      width: 100%;
      min-height: 260px;
      overflow-y: auto;
      padding: 10px;
      box-sizing: border-box;

      .loading-msg-panel {
        width: 100%;
        height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;

        img {
          height: 100%;
          user-select: none;
          animation: loading-msg 1s linear;
          // 动画无限循环
          animation-iteration-count: infinite;
        }
      }

      // 加载消息动画
      @keyframes loading-msg {
        0% {
          transform: rotate(0deg);
        }

        50% {
          transform: rotate(180deg);
        }

        100% {
          transform: rotate(360deg);
        }
      }

      .row-panel {
        width: 100%;
        min-height: 50px;

        /*对方消息样式*/
        .otherSide-panel {
          width: 96%;
          min-height: 50px;
          display: flex;
          margin-bottom: 15px;
          position: relative;

          .avatar-panel {
            width: 30px;
            min-width: 30px;
            height: 30px;
            border-radius: 50%;
            user-select: none;
            overflow: hidden;

            img {
              width: 100%;
              height: 100%;
              user-select: none;
            }
          }

          .user-name-panel {
            width: 240px;
            height: auto;
            position: absolute;
            left: 42px;
            top: 2px;
            display: flex;
            justify-content: flex-start;
            align-items: center;

            p {
              color: #9da9c6;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            }
          }

          .msg-body {
            max-width: 95%;
            min-height: 40px;
            overflow: hidden;
            background: #f4f3f3;
            border-radius: 5px;
            padding: 10px;
            box-sizing: border-box;
            margin-top: 28px;
            margin-left: 16px;
            position: relative;

            /*消息尾巴*/
            .tail-panel {
              width: 20px;
              height: 100%;
              position: absolute;
              left: -14px;

              svg {
                margin-top: 8px;
                color: #f3f3f3;
              }
            }

            p {
              font-size: 15px;
              /*自动换行*/
              word-wrap: break-word;
              overflow: hidden;

              img {
                width: 100%;
                height: 100%;
                user-select: none;
              }
            }
          }
        }

        .sender-time-panel {
          width: 100%;
          height: 30px;
          display: flex;
          justify-content: center;
          align-items: center;

          span {
            color: #CDCCCD;
          }
        }

        /*发送者消息样式*/
        .sender-panel {
          width: 96%;
          min-height: 50px;
          float: right;
          margin-right: 12px;
          display: flex;
          justify-content: flex-end;
          margin-bottom: 15px;
          position: relative;

          .avatar-panel {
            width: 30px;
            min-width: 30px;
            height: 30px;
            user-select: none;
            border-radius: 50%;
            overflow: hidden;

            img {
              width: 100%;
              height: 100%;
              user-select: none;
            }
          }

          .user-name-panel {
            width: 240px;
            height: auto;
            position: absolute;
            right: 42px;
            top: 2px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: flex;
            justify-content: flex-end;
            align-items: center;

            p {
              color: #9da9c6;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            }
          }

          .msg-body {
            max-width: 95%;
            min-height: 40px;
            background: #d8e8dc;
            overflow: hidden;
            border-radius: 5px;
            display: flex;
            align-items: center;
            padding: 10px;
            box-sizing: border-box;
            margin-top: 28px;
            margin-right: 16px;
            position: relative;

            /*消息尾巴*/
            .tail-panel {
              width: 20px;
              height: 100%;
              position: absolute;
              right: -18px;

              svg {
                margin-top: 8px;
                color: #f3f3f3;
              }
            }

            p {
              font-size: 15px;
              line-height: 23px;
              // 强制换行
              word-break: break-all;

              img {
                width: 100%;
                height: 100%;
                display: block;
                user-select: none;
              }
            }
          }
        }
      }
    }

    .user-input-panel {
      width: 100%;
      height: 160px;
      position: relative;

      .toolbar-panel {
        width: 100%;
        height: 40px;
        display: flex;
        align-items: center;
        padding-left: 10px;

        .item-panel {
          width: 24px;
          height: 24px;
          margin-right: 20px;
          display: flex;
          justify-content: center;
          align-items: center;

          .file-panel {
            width: 24px;
            height: 24px;
            position: relative;

            input[type="file"] {
              width: 24px;
              height: 24px;
              opacity: 0;
              position: absolute;
              left: 0;
              top: 0;
            }
          }

          img {
            width: 100%;
            height: 100%;
            user-select: none;
          }
        }

        .send-panel {
          width: 70px;
          height: 30px;
          background-image: linear-gradient(-90deg, #29bdd9 0%, #276ace 100%);
          text-align: center;
          color: white;
          border-radius: 5px;
          line-height: 30px;
          display: none;
        }
      }

      .input-panel {
        width: 100%;
        min-height: 30px;
        max-height: 120px;
        overflow-y: auto;
        outline: none;
        display: flex;
        align-items: center;
        flex-flow: row wrap;
        padding: 10px;
        box-sizing: border-box;
        // 强制换行
        word-break: break-all;
      }

      /*表情面板*/
      .emoticon-panel {
        width: 416px;
        height: 270px;
        border-radius: 5px;
        background: white;
        border: solid 1px #dfe0e0;
        padding: 20px;
        box-sizing: border-box;
        position: absolute;
        top: -268px;
        left: -186px;
        display: flex;
        justify-content: center;

        .container {
          overflow-y: auto;

          .row-panel {
            width: 100%;
            min-height: 30px;
            display: flex;
            align-items: center;
            flex-flow: row wrap;

            .item-panel {
              width: 25px;
              height: 25px;
              margin-right: 6px;
              margin-bottom: 7px;
              position: relative;

              // 取消12的倍数的元素的右外边距
              &:nth-child(12n) {
                margin-right: 0;
              }

              img {
                width: 100%;
                height: 100%;
                user-select: none;

                &:hover {
                  width: 26px;
                  height: 26px;
                }
              }
            }
          }
        }

        .ico-panel {
          width: 0;
          height: 0;
          border-right: 6px solid transparent;
          border-left: 6px solid transparent;
          border-top: 6px solid #dfe0e0;
          position: absolute;
          bottom: -6px;

          img {
            width: 100%;
            height: 100%;
            user-select: none;
          }
        }
      }
    }
  }
</style>
